/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ModelJava;

import java.security.MessageDigest;

/**
 *
 * @author papa
 */
public class SHA1hash {

    private static byte[] output;

//    public static void main(String[] a) {
//      String tom = "tomas";
//        System.out.println(getSHA1(tom));
//   }

    public static String getSHA1(String input){
        try {
         MessageDigest md = MessageDigest.getInstance("SHA1");

         md.update(input.getBytes());
      	 output = md.digest();
         return bytesToHex(output);

      } catch (Exception e) {
         System.out.println("Exception: "+e);
         return input;
      }
    }

   private static String bytesToHex(byte[] b) {
      char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
      StringBuffer buf = new StringBuffer();
      for (int j=0; j<b.length; j++) {
         buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
         buf.append(hexDigit[b[j] & 0x0f]);
      }
      return buf.toString();
   }

}
